package com.example.njupt.uis.flutter;

import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.njupt.R;
import com.example.njupt.base.BaseActivity;
import com.example.njupt.constant.ARouteConstance;

import io.flutter.embedding.android.FlutterFragment;

/**
 * Author:      mwang
 * Create Date: Created in 2021/6/3 10:36
 * Update Date:
 * Modified By:
 * Description:
 */
@Route(path = ARouteConstance.ACTIVITY_FLUTTER)
public class FlutterActivity extends BaseActivity {

    private FlutterFragment fragment;
    public static final String TAG_FLUTTER_FRAGMENT = "flutter_fragment";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_flutter;
    }

    @Override
    protected void initView() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragment = (FlutterFragment) fragmentManager.findFragmentByTag(TAG_FLUTTER_FRAGMENT);
        if (fragment == null) {
            fragment = FlutterFragment.createDefault();
            fragmentManager.beginTransaction().add(
                    R.id.fragment_container,
                    fragment,
                    TAG_FLUTTER_FRAGMENT
            ).commit();
        }

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    public void onPostResume() {
        super.onPostResume();
        fragment.onPostResume();
    }

    @Override
    protected void onNewIntent(@NonNull Intent intent) {
        super.onNewIntent(intent);
        fragment.onNewIntent(intent);
    }

    @Override
    public void onBackPressed() {
        fragment.onBackPressed();
    }

    @Override
    public void onRequestPermissionsResult(
            int requestCode,
            @NonNull String[] permissions,
            @NonNull int[] grantResults
    ) {
        fragment.onRequestPermissionsResult(
                requestCode,
                permissions,
                grantResults
        );
    }

    @Override
    public void onUserLeaveHint() {
        fragment.onUserLeaveHint();
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        fragment.onTrimMemory(level);
    }
}
